package com.tanhua.server.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

import java.lang.reflect.Method;

/**
 * 定制化CachingConfigurer
 *
 * @author JustryDeng
 * @date 2019/4/11 16:26
 */
@Configuration
public class MyCachingConfigurer extends CachingConfigurerSupport {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    /**
     * 配置一个CacheManager才能使用@Cacheable等注解
     *
     * 公众号：MarkerHub
     */
    /*@Bean
    public CacheManager cacheManager(RedisTemplate<String, Object> template) {

        // 基本配置
        RedisCacheConfiguration defaultCacheConfiguration =
                RedisCacheConfiguration
                        .defaultCacheConfig()
                        // 设置key为String
                        .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(template.getStringSerializer()))
                        // 设置value 为自动转Json的Object
                        .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(template.getValueSerializer()))
                        // 不缓存null
                        .disableCachingNullValues()
                        // 缓存数据保存5分钟
                        .entryTtl(Duration.ofMinutes(5));

        // 够着一个redis缓存管理器

        return RedisCacheManager.RedisCacheManagerBuilder
                // Redis 连接工厂
                .fromConnectionFactory(Objects.requireNonNull(template.getConnectionFactory()))
                // 缓存配置
                .cacheDefaults(defaultCacheConfiguration)
                // 配置同步修改或删除 put/evict
                .transactionAware()
                .build();
    }*/

    /**
     * 定制化key生成器
     * <p>
     * 设置  全限定类名 + 方法名 + 参数名 共同组成 key
     *
     * @return key生成器
     * @date 2019/4/12 14:09
     */
    @Bean
    @Override
    public KeyGenerator keyGenerator() {
        return (Object target, Method method, Object... params) -> {
            StringBuilder sb = new StringBuilder(16);
            sb.append(target.getClass().getName());
            sb.append("_");
            sb.append(method.getName());
            sb.append("_");
            for (int i = 0; i < params.length; i++) {
                sb.append(params[i]);
                if (i < params.length - 1) {
                    sb.append(",");
                }
            }

            /*System.out.println(sb);
            System.out.println("test1234?? " + redisTemplate.opsForValue().get("test123"));
            System.out.println("test?? " + redisTemplate.opsForValue().get("test"));
            return "test1234";*/

            return sb.toString();
        };
    }
}